package com.alan.wifishare.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.util.Log;
  
  
public class WifiUtil {  
      
    private static final String TAG = "WifiAdmin";  
      
    private WifiManager mWifiManager;  
    private WifiInfo mWifiInfo;  
    // 扫描出的网络连接列表  
    private List<ScanResult> mWifiList;  
    private List<WifiConfiguration> mWifiConfiguration;  
  
    private WifiLock mWifiLock;  
      
  public static final int TYPE_NO_PASSWD = 0x11;  
  public static final int TYPE_WEP = 0x12;  
  public static final int TYPE_WPA = 0x13;  
    
    public WifiUtil(Context context) {  
          
        // 取得WifiManager对象  
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
        // 取得WifiInfo对象  
        mWifiInfo = mWifiManager.getConnectionInfo();  
          
        Log.v(TAG, "getIpAddress = " + mWifiInfo.getIpAddress());  
    }  
    // setWifiEnabled : false
    // 打开WIFI  
    public boolean isWifiEnabled() {  
        if (mWifiManager.isWifiEnabled()) {  
            return true; 
        }  
        return false;
    }
    
    // 打开WIFI  
    public void openWifi() { 
    	closeWifiAp();
        if (!mWifiManager.isWifiEnabled()) {  
            mWifiManager.setWifiEnabled(true);  
        }  
    }  
  
    // 关闭WIFI  
    public void closeWifi() {  
        if (mWifiManager.isWifiEnabled()) {  
            mWifiManager.setWifiEnabled(false);  
        }  
    }  
      
    
    public void connectToWifi(String ssid, String passwd, int type) {  
        if (ssid == null || passwd == null || ssid.equals("")) {  
            Log.e(TAG, "addNetwork() ## nullpointer error!");  
            return;  
        }  
        if (type != TYPE_NO_PASSWD && type != TYPE_WEP && type != TYPE_WPA) {  
            Log.e(TAG, "addNetwork() ## unknown type = " + type);  
        }  
        addNetwork(createWifiInfo(ssid, passwd, type));  
    }  

    
    // 添加一个网络并连接  
    private void addNetwork(WifiConfiguration wcg) {  
        int wcgID = mWifiManager.addNetwork(wcg);  
        mWifiManager.enableNetwork(wcgID, true);  
    }  
    
      
      
    private WifiConfiguration createWifiInfo(String SSID, String password, int type) {  
          
        Log.v(TAG, "SSID = " + SSID + "## Password = " + password + "## Type = " + type);  
          
        WifiConfiguration config = new WifiConfiguration();  
        config.allowedAuthAlgorithms.clear();  
        config.allowedGroupCiphers.clear();  
        config.allowedKeyManagement.clear();  
        config.allowedPairwiseCiphers.clear();  
        config.allowedProtocols.clear();  
        config.SSID = "\"" + SSID + "\"";  
  
        WifiConfiguration tempConfig = this.getConfiguration(SSID);  
        if (tempConfig != null) {  
            mWifiManager.removeNetwork(tempConfig.networkId);  
        }  
          
        // 分为三种情况：1没有密码2用wep加密3用wpa加密  
        if (type == TYPE_NO_PASSWD) {// WIFICIPHER_NOPASS  
            config.wepKeys[0] = "";  
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
            config.wepTxKeyIndex = 0;  
              
        } else if (type == TYPE_WEP) {  //  WIFICIPHER_WEP   
            config.hiddenSSID = true;  
            config.wepKeys[0] = "\"" + password + "\"";  
            config.allowedAuthAlgorithms  
                    .set(WifiConfiguration.AuthAlgorithm.SHARED);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);  
            config.allowedGroupCiphers  
                    .set(WifiConfiguration.GroupCipher.WEP104);  
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
            config.wepTxKeyIndex = 0;  
        } else if (type == TYPE_WPA) {   // WIFICIPHER_WPA  
            config.preSharedKey = "\"" + password + "\"";  
            config.hiddenSSID = true;  
            config.allowedAuthAlgorithms  
                    .set(WifiConfiguration.AuthAlgorithm.OPEN);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);  
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);  
            config.allowedPairwiseCiphers  
                    .set(WifiConfiguration.PairwiseCipher.TKIP);  
            // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
            config.allowedPairwiseCiphers  
                    .set(WifiConfiguration.PairwiseCipher.CCMP);  
            config.status = WifiConfiguration.Status.ENABLED;  
        }   
          
        return config;  
    }  
      
    public static final int WIFI_CONNECTED = 0x01;  
    public static final int WIFI_CONNECT_FAILED = 0x02;  
    public static final int WIFI_CONNECTING = 0x03;  
    /** 
     * 判断wifi是否连接成功,不是network 
     *  
     * @param context 
     * @return 
     */  
    public int getWifiContectedState(Context context) {  
        ConnectivityManager connectivityManager = (ConnectivityManager) context  
                .getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo wifiNetworkInfo = connectivityManager  
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
          
        Log.v(TAG, "isConnectedOrConnecting = " + wifiNetworkInfo.isConnectedOrConnecting());  
        Log.d(TAG, "wifiNetworkInfo.getDetailedState() = " + wifiNetworkInfo.getDetailedState());  
        if (wifiNetworkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR  
                || wifiNetworkInfo.getDetailedState() == DetailedState.CONNECTING) {  
            return WIFI_CONNECTING;  
        } else if (wifiNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {  
            return WIFI_CONNECTED;  
        } else {  
            Log.d(TAG, "getDetailedState() == " + wifiNetworkInfo.getDetailedState());  
            return WIFI_CONNECT_FAILED;  
        }  
    }  
      
    private WifiConfiguration getConfiguration(String SSID) {  
        List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();  
        if(existingConfigs == null)
        {
            Log.i("Tag", "existingConfigs == null"); 
        }
        if(existingConfigs!=null)
        {
            Log.i("Tag", "existingConfigs != null");
        	Log.i("Tag", "existingConfigs.size()->" + existingConfigs.size());
        	if(existingConfigs.size()>0)
            {
        		for (WifiConfiguration existingConfig : existingConfigs) 
        		{  
//                	Log.i("Tag", "existingConfig.SSID->" + existingConfig.SSID);
        			if (existingConfig.SSID.equals(SSID) /*&& existingConfig.preSharedKey.equals("\"" + password + "\"")*/) 
        			{  
        				return existingConfig;  
        			}
        		}
            }
        }  
        return null;  
    }  
      
  
  
    // 断开指定ID的网络  
    public void disconnectWifi(int netSSID) {  
        mWifiManager.disableNetwork(netSSID);  
        mWifiManager.disconnect();  
    }  
      
    // 检查当前WIFI状态  
    public int checkState() {  
        return mWifiManager.getWifiState();  
    }  
  
    // 锁定WifiLock  
    public void acquireWifiLock() {  
        mWifiLock.acquire();  
    }  
  
    // 解锁WifiLock  
    public void releaseWifiLock() {  
        // 判断时候锁定  
        if (mWifiLock.isHeld()) {  
            mWifiLock.acquire();  
        }  
    }  
  
    // 创建一个WifiLock  
    public void creatWifiLock() {  
        mWifiLock = mWifiManager.createWifiLock("Test");  
    }  
  
    // 得到配置好的网络  
    public List<WifiConfiguration> getConfiguration() {  
        return mWifiConfiguration;  
    }  
  
    // 指定配置好的网络进行连接  
    public void connectConfiguration(int index) {  
        // 索引大于配置好的网络索引返回  
        if (index > mWifiConfiguration.size()) {  
            return;  
        }  
        // 连接配置好的指定ID的网络  
        mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true);  
    }  
  
    public void startScan() {  
        mWifiManager.startScan();  
        mWifiList = mWifiManager.getScanResults();  
        mWifiConfiguration = mWifiManager.getConfiguredNetworks();  
    }  
  
    // 得到网络列表  
    public List<ScanResult> getWifiList() {  
        return mWifiList;  
    }  
  
    // 查看扫描结果  
    public StringBuilder lookUpScan() {  
        StringBuilder stringBuilder = new StringBuilder();  
        for (int i = 0; i < mWifiList.size(); i++) {  
            stringBuilder.append("Index_" + new Integer(i + 1).toString() + ":");  
            // 将ScanResult信息转换成一个字符串包  
            // 其中把包括：BSSID、SSID、capabilities、frequency、level  
            stringBuilder.append((mWifiList.get(i)).toString());  
            stringBuilder.append("/n");  
        }  
        return stringBuilder;  
    }  
  
    // 得到MAC地址  
    public String getMacAddress() {  
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();  
    }  
    
    // 得到接入点的SSID  
    public String getSSID() {  
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getSSID();  
    }  
    
    // 得到接入点的BSSID  
    public String getBSSID() {  
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();  
    }  
  
    // 得到IP地址  
    public int getIPAddress() {  
        return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();  
    }  
  
    // 得到连接的ID  
    public int getNetworkId() {  
        return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();  
    }  
  
    // 得到WifiInfo的所有信息包  
    public WifiInfo getWifiInfo() {
        return mWifiManager.getConnectionInfo();
    }
    // 得到WifiInfo的所有信息包
    public String getWifiInfoStr() {
        mWifiInfo = mWifiManager.getConnectionInfo();
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
    }

    public List<ScanResult> getScanResults() {
		return mWifiManager.getScanResults();
	}
    
    public boolean isWifiExist(String SSID) {
    	mWifiInfo = mWifiManager.getConnectionInfo();  
    	String curSSID = mWifiInfo.getSSID();//带双引号
    	String ssid = curSSID.substring(1, curSSID.length()-1);
    	
//    	Log.i("Tag", "onStateChanged()---->SSID = "+SSID);
//    	Log.i("Tag", "onStateChanged()---->ssid = "+ssid);
    	if (ssid.equals(SSID)) {
    		return true;
		}
    	return false;
	}
    
    /** 
     * 热点开关是否打开 
     * @return 
     */  
    public boolean isWifiApEnabled() {    
        try {    
            Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");    
            method.setAccessible(true);    
            return (Boolean) method.invoke(mWifiManager);    
        } catch (NoSuchMethodException e) {    
            e.printStackTrace();    
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
        return false;    
    }    
      
    /** 
     * 关闭WiFi热点 
     */  
    public void closeWifiAp() {    
        if (isWifiApEnabled()) {    
            try {    
                Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");    
                method.setAccessible(true);    
                WifiConfiguration config = (WifiConfiguration) method.invoke(mWifiManager);    
                Method method2 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);    
                method2.invoke(mWifiManager, config, false);    
            } catch (NoSuchMethodException e) {    
                e.printStackTrace();    
            } catch (IllegalArgumentException e) {    
                e.printStackTrace();    
            } catch (IllegalAccessException e) {    
                e.printStackTrace();    
            } catch (InvocationTargetException e) {    
                e.printStackTrace();    
            }    
        }   
    }  
    
} 
